package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.OutboundDetailMapper;
import com.spark.incoicings.dao.OutboundOrderMapper;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.service.OutboundOrderService;
import com.spark.incoicings.utils.RandomGenerateUtil;
import com.spark.invoicings.entity.Code;
import com.spark.invoicings.entity.OutboundDetail;
import com.spark.invoicings.entity.OutboundOrder;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OutboundOrderServiceImpl extends ServiceImpl<OutboundOrderMapper, OutboundOrder> implements OutboundOrderService {

    @Resource
    private OutboundOrderMapper outboundOrderMapper;

    @Resource
    private OutboundDetailMapper outboundDetailMapper;

    @Resource
    private RandomGenerateUtil randomGenerateUtil;

    @Resource
    private ProductMapper productMapper;

    /**
     * 根据id获取出库单
     * @param id
     * @return
     */
    @Override
    public OutboundOrder selectOutboundOrderById(Integer id) {
        OutboundOrder outboundOrder = outboundOrderMapper.selectOutboundOrderById(id);
        List<OutboundDetail> outboundDetails = outboundDetailMapper.selectOutboundDetailByIds(outboundOrder.getOutboundId());
        outboundOrder.setOutboundDetailList(outboundDetails);
        return outboundOrder;
    }

    /**
     * 更新出库单
     * @param outboundOrder
     * @return
     */
    @Transactional
    @Override
    public Result updateOutboundOrder(OutboundOrder outboundOrder) {
        int totalQuantity = 0;
        for (OutboundDetail outboundDetail : outboundOrder.getOutboundDetailList()) {
            totalQuantity += outboundDetail.getOutboundQuantity();
            outboundDetailMapper.updateById(outboundDetail);
            // 出库数量
            Product product = productMapper.selectProductById(outboundDetail.getProductId());
            // 产品库存
            product.setCurrentInventory(product.getCurrentInventory() - outboundDetail.getOutboundQuantity());
            productMapper.updateById(product);
        }
        // 计算出库总量
        outboundOrder.setTotalQuantity(totalQuantity);
        outboundOrderMapper.updateById(outboundOrder);
        return Result.success(200, "success", outboundOrder.getOutboundId());
    }

    /**
     * 保存出库单
     * @param outboundOrder
     * @return
     */
    @Transactional
    @Override
    public Result addOutboundOrder(OutboundOrder outboundOrder) {
        // 生成出库单
        Code code = new Code();
        code.setType("CK");
        code.setText(code.getType() + "%s%s");
        String ckId = randomGenerateUtil.nextSerialNumber(code);
        outboundOrder.setOutboundNumber(ckId);
        int totalQuantity = 0;
        for (OutboundDetail outboundDetail : outboundOrder.getOutboundDetailList()) {
            totalQuantity += outboundDetail.getOutboundQuantity();
            outboundDetailMapper.insert(outboundDetail);
            // 出库数量
            Product product = productMapper.selectProductById(outboundDetail.getProductId());
            // 产品库存
            product.setCurrentInventory(product.getCurrentInventory() - outboundDetail.getOutboundQuantity());
            productMapper.updateById(product);
        }
        // 计算出库总量
        outboundOrder.setTotalQuantity(totalQuantity);
        outboundOrderMapper.insert(outboundOrder);
        for (OutboundDetail outboundDetail : outboundOrder.getOutboundDetailList()) {
            outboundDetail.setOutboundOrderId(outboundOrder.getOutboundId());
            outboundDetailMapper.updateById(outboundDetail);
        }
        return Result.success(200, "success", outboundOrder.getOutboundId());
    }

    /**
     * 查询所有出库单
     * @param map
     * @return
     */
    @Override
    public List<OutboundOrder> selectOutboundOrderAll(Map<String, Object> map) {
        return outboundOrderMapper.selectOutboundOrderAll(map);
    }

}
